package com.dragon.logical.service.auth.impl;

import cn.hutool.core.date.DateUtil;
import com.dragon.common.model.ResultCode;
import com.dragon.common.model.exception.CustomException;
import com.dragon.repository.entity.Dictsort;
import com.dragon.repository.mapper.DictsortMapper;
import com.dragon.logical.service.auth.IDictsortService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 字典分类 服务实现类
 * </p>
 *
 * @author sy
 * @since 2023-06-27
 */
@Service
public class DictsortServiceImpl extends ServiceImpl<DictsortMapper, Dictsort> implements IDictsortService {


    @Autowired
    DictsortMapper mapper;


    @Override
    public List<Dictsort> selectAllList(){
        return mapper.selectList(null);
    }

    @Override
    public Integer addDictsort(Dictsort dictsort){
        dictsort.setCreateTime(DateUtil.date());
        mapper.insert(dictsort);
        return dictsort.getId();
    }

    @Override
    public void updateDictsort(Dictsort dictsort){
        Dictsort dictsort1 = mapper.selectById(dictsort.getId());
        if(dictsort1 == null){
            throw new CustomException(ResultCode.DATA_NOT_FOUND);
        }
        dictsort.setUpdateTime(DateUtil.date());
        mapper.updateById(dictsort);
    }

    @Override
    public void deleteDictsort(Integer id){
        Dictsort dictsort = mapper.selectById(id);
        if(dictsort == null){
            throw new CustomException(ResultCode.DATA_NOT_FOUND);
        }
        mapper.deleteById(dictsort);
    }
}
